/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import model.HinhThucHopDong;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import util.HibernateUtil;

/**
 *
 * @author MRDO
 */
public class HinhThucHopDongDao {
    private static Session session;
    
    public HinhThucHopDongDao(){
        SessionFactory factory = HibernateUtil.getSessionFactory();
        this.session = factory.getCurrentSession();
    }
    
    /*
     * insertHinhThuc
     */
    public boolean insertHinhThuc(String tenHinhThuc){
        try {
            session.beginTransaction();
            
            HinhThucHopDong hinhThuc = new HinhThucHopDong(tenHinhThuc);
            
            session.save(hinhThuc);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        }
        return false;
    }
    
    /*
     * updateHinhThuc
     */
    public boolean updateHinhThuc(int id, String ten){
        try {
            session.beginTransaction();
            
            HinhThucHopDong hinhThuc = (HinhThucHopDong)session.get(HinhThucHopDong.class, id);
            hinhThuc.setTenHinhThuc(ten);
            
            session.update(hinhThuc);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        }
        return false;
    }
    
    /*
     * deleteHinhThuc
     */
    public boolean deleteHinhThuc(int id){
        try {
            session.beginTransaction();
            
            HinhThucHopDong hinhThuc = (HinhThucHopDong)session.get(HinhThucHopDong.class, id);
            
            session.delete(hinhThuc);
            session.flush();
            session.getTransaction().commit();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        }
        return false;
    }
    
    /*
     * 
     */
}
